package com.trainoo.controller;

import com.trainoo.repository.GirlRepository;
import com.trainoo.domain.Girl;
import com.trainoo.service.GirlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by Administrator on 2017/6/5.
 */
@RestController
public class GirlController {

    @Autowired
    private GirlRepository girlRepository;

    @Autowired
    private GirlService girlService;

    @GetMapping(value="girls")
    public List<Girl> getGirls(){
        return girlRepository.findAll();
    }

    /*
     *   @Valid对数据进行校验
     */
    @GetMapping(value="save")
    public Girl save(@Valid Girl girl, BindingResult result){
        if(result.hasErrors()){
            System.out.println(result.getFieldError().getDefaultMessage());
            return null;
        }
        girl.setAge(girl.getAge());
        girl.setCupSize(girl.getCupSize());
        return girlRepository.save(girl);
    }

    @GetMapping(value="save01")
    public void save_01(){
        girlService.insertTwo();
    }

    @GetMapping(value="find")
    public void find() throws Exception {
        girlService.getOne();
    }

}
